package interfaze;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.SwingConstants;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import logic.BasicPlayerTest;

public class PlayWindow extends JFrame implements ActionListener,
		MouseListener, WindowListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel btnPrv, btnPlay, btnNxt;
	JButton btnRwd, btnFwd;
	JSlider slider;
	// el lblCancion debe ser cambiado con el formato "Artista - Canción" cuando
	// se cambie de pista
	protected JLabel lblCancion;
	private JPanel panel_5;
	private JPanel panel_6;
	private JButton btnBiblioteca;
	private ListWindow padre;
	Thread checker = new hiloChecker();
	boolean performCheck = true;
	Color background = Color.black;
	BasicPlayerTest player = new BasicPlayerTest();
	File song;
	boolean playing = false;
	boolean first = true;

	public PlayWindow(ListWindow p, File file) {
		song = file;
		padre = p;
		getContentPane().setBackground(background);
		getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(background);
		getContentPane().add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(background);
		panel_3.add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		lblCancion = new JLabel("Artista - Cancion");
		lblCancion.setForeground(Color.WHITE);
		panel.add(lblCancion);

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(background);
		panel_3.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(background);
		panel_4.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		slider = new JSlider();
		slider.setBackground(background);
		slider.setValue(0);
		panel_2.add(slider);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(background);
		panel_4.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		btnPrv = new JLabel("");
		btnPrv.setForeground(background);
		btnPrv.setBackground(background);
		btnPrv.setIcon(new ImageIcon("previous.png"));
		panel_1.add(btnPrv);

		btnRwd = new JButton("");
		panel_1.add(btnRwd);

		btnPlay = new JLabel("");
		btnPlay.setForeground(background);
		btnPlay.setBackground(background);
		btnPlay.setIcon(new ImageIcon("play.png"));
		panel_1.add(btnPlay);

		btnFwd = new JButton("");
		panel_1.add(btnFwd);

		btnNxt = new JLabel("");
		btnNxt.setForeground(background);
		btnNxt.setBackground(background);
		btnNxt.setIcon(new ImageIcon("next.png"));
		panel_1.add(btnNxt);

		panel_5 = new JPanel();
		panel_5.setBackground(background);
		getContentPane().add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new BorderLayout(0, 0));

		panel_6 = new JPanel();
		panel_6.setBackground(background);
		panel_5.add(panel_6, BorderLayout.WEST);
		btnBiblioteca = new JButton("Biblioteca");
		panel_6.add(btnBiblioteca);
		btnBiblioteca.setHorizontalAlignment(SwingConstants.LEFT);

		JLabel lblIcon = new JLabel();
		lblIcon.setIcon(new ImageIcon("player.png"));
		panel_5.add(lblIcon, BorderLayout.EAST);
		lblIcon.setHorizontalAlignment(SwingConstants.RIGHT);

		btnPrv.addMouseListener(this);

		btnRwd.addActionListener(this);
		btnRwd.addMouseListener(this);
		btnRwd.setVisible(false);

		btnPlay.addMouseListener(this);

		btnFwd.addActionListener(this);
		btnFwd.addMouseListener(this);
		btnFwd.setVisible(false);

		btnNxt.addMouseListener(this);

		btnBiblioteca.addActionListener(this);

		this.setSize(450, 160);
		this.setMinimumSize(new Dimension(450, 160));
		this.setResizable(false);
		btnBiblioteca.setVisible(false);
		checker.start();
	}

	public PlayWindow() {
		// TODO Auto-generated constructor stub
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource().equals(btnPlay)) {
			
			if (!playing && first) {
				player.open(song);
				player.play();
				playing = true;
				first = false;
			} else if (playing) {
				player.pause();
				playing = false;
			} else if (!playing && !first) {
				player.resume();
				playing = true;
			}
		} else if (arg0.getSource().equals(btnNxt)) {
			
			player.open(song);
			if (playing) {
				player.play();
			} else {
				playing = false;
			}
		} else if (arg0.getSource().equals(btnPrv)) {
			
			player.open(song);
			if (playing) {
				player.play();
			} else {
				playing = false;
			}
		}
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnPrv)) {

		} else if (e.getSource().equals(btnRwd)) {

		} else if (e.getSource().equals(btnPlay)) {

		} else if (e.getSource().equals(btnFwd)) {

		} else if (e.getSource().equals(btnNxt)) {

		}
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnPrv)) {

		} else if (e.getSource().equals(btnRwd)) {

		} else if (e.getSource().equals(btnPlay)) {

		} else if (e.getSource().equals(btnFwd)) {

		} else if (e.getSource().equals(btnNxt)) {

		}
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnPrv)) {
			player.open(song);
			if (btnPlay.getText().equals("pause")) {
				player.play();
			} else {
				btnPlay.setText("ply");
			}
		} else if (e.getSource().equals(btnBiblioteca)) {
			if (padre == null) {
				padre = new ListWindow();
				padre.setLocation(this.getLocation());
				padre.setVisible(true);
			} else {
				if (!padre.isVisible()) {
					padre.setVisible(true);
				}
			}
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		player.stop();

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	class hiloChecker extends Thread {
		public void run() {
			check();
		}
	}

	public void check() {
		while (performCheck) {
			
			try {
				checker.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!padre.isVisible() || padre == null) {
				
				btnBiblioteca.setVisible(true);
				((JPanel) this.getContentPane()).updateUI();
			} else {
				
				btnBiblioteca.setVisible(false);
			}
		}
	}
}
